﻿var agentClient = null;
var timer = null;
var seconds = 0;
var timerDirection = "up";
var agentLog = null;
var isAdviceCancel = false; //取消转移标识
var isTransfer = false; //转接咨询
var isConference = false; //会议咨询
$(function(){
    var lastRecordId = "";

    agentClient = new AgentClient({
        agentType: 1,
        skillDesc: $("#skillDesc").val(),
        autoReady: 2,
        acwTime:   100, //2分钟
        dialPrefix: "9",
        /*proxyUrl:  $("#CTIServicePath").val(),*/
        proxyUrl:  'http://172.16.1.101:10086',
        owReasons: ["小休", "培训", "就餐"]
    });

    //连线成功
    agentClient.onOnline = function(event) {
        console.log("上线了、上线了、");
    };

    //离线
    agentClient.onOffline = function(event) {
        console.log("离线了、离线了、");
    };

    //状态更新
    agentClient.onStatusChanged = function(event) {
        //event.eventType,event.currStatus,event.reasonCode,event.startTime
        console.log(event);
        switch (event.currStatus) {
            case 1 : { //签入成功
                loginSuccess();
                break;
            }
            case 8 : { //签出成功
                logoutSuccess();
                break;
            }
            case 2 : { //置忙成功
                notReadySuccess();
                break;
            }
            case 3 : { //就绪成功
                readySuccess();
                break;
            }
            case 6 : { //话后处理成功
                if(event.reasonCode != 27){ //排除掉咨询通话的挂断
                    afterCallWorkSuccess();
                }
                break;
            }
            case 7 : { //其它工作成功
                switch (event.reasonCode) {
                    case 70 : { //小休
                        restSuccess();
                        break;
                    }
                    case 71 : { //培训
                        trainSuccess();
                        break;
                    }
                    case 72 : { //就餐
                        mealSuccess();
                        break;
                    }
                }
                break;
            }
            case 25 : {
                if(event.reasonCode == 25){ //咨询未接通取消
                    isAdviceCancel = true; //取消咨询标识
                    if(isTransfer) { //转接咨询取消
                        transferCancelSuccess();
                    }
                    if(isConference) { //会议咨询取消
                        conferenceCancelSuccess();
                    }
                }
                break;
            }
            case 27 : {
                if(isTransfer) {//转接咨询成功
                    adviceSuccess();
                }
                if(isConference) { //会议咨询成功
                    conferenceAdviceSuccess();
                }
                break;
            }
            case 28 : {
                if(event.reasonCode == 23){ //保持恢复成功
                    retrievedSuccess();
                }
                break;
            }
            case 29 : {
                if(event.reasonCode == 17){ //保持成功
                    holdSuccess();
                }
                break;
            }
            case 31 : {
                if(isTransfer) {
                    adviceConnectedSuccess(); //转接咨询通话成功
                }
                if(isConference){ //会议咨询通话成功
                    conferenceConnectedSuccess();
                }
                break;
            }
            case 33 : { //会议成功
                conferenceSuccess();
                break;
            }
        }
    };

    //呼出振铃
    agentClient.onDialing = function(event) {
        lastRecordId = Math.uuid();
        //如果是外线且不是外线咨询则弹屏
        //if(event.callType == 1 && event.lastStatus != 27) {
            $("#acceptPhoneBtn").hide();
            $("#releasePhoneBtn").show();
            $("#signOutBtn, #readyBtn, #statusMenuBtn, #dailPhoneBtn," +
                " #holdPhoneBtn, #transferPhoneBtn, #skillGroupBtn, #conferencePhoneBtn").attr("disabled", true);
            $("#statusInfo").html("拨号");
            var customerNumber = event.oriDnis;
            if(customerNumber.indexOf($("#CTICallPrefix").val()) == 0){ //去掉外呼前缀
                customerNumber = customerNumber.substr($("#CTICallPrefix").val().length);
            }
            if(customerNumber.indexOf("9") == 0){ //去掉外呼前缀为9的
                customerNumber = customerNumber.substr(1);
            }
            if (customerNumber.indexOf("01") == 0 && customerNumber.length == 12) { //去掉外外地手机前面加拨的0
                customerNumber = customerNumber.substr(1);
            }
            $("#outPhoneNumber").html(customerNumber);
            popUpWindow(lastRecordId, event.contactId, agentClient.getOption("agentId"), agentClient.getOption("agentDn"), event.startTime, event.oriAni, customerNumber, "2", "-1");
            // agentLog.sendMessage(agentLog.DIALING, customerNumber, "-1", "2");
        //}
    };

    //呼入振铃
    agentClient.onOffering = function(event) {
        lastRecordId = Math.uuid();
        //外线且不是咨询振铃则弹
        //if(event.callType == 1 && event.currStatus != 24) {
            $("#acceptPhoneBtn").show().attr("disabled", false);
            $("#releasePhoneBtn").hide();
            $("#signOutBtn, #readyBtn, #statusMenuBtn, #dailPhoneBtn," +
                " #holdPhoneBtn, #transferPhoneBtn, #skillGroupBtn, #conferencePhoneBtn").attr("disabled", true);
            var customerNumber = event.oriAni;
            if (customerNumber.indexOf("01") == 0 && customerNumber.length == 12) { //去掉外外地手机前面加拨的0
                customerNumber = customerNumber.substr(1);
            }
            $("#inPhoneNumber").html(customerNumber);
            $("#statusInfo").html("来电");
            callerUserInfo(lastRecordId,customerNumber);
            established(lastRecordId, event.contactId, event.startTime,agentClient.getOption("agentId"),customerNumber);
            //popUpWindow(lastRecordId, event.contactId, agentClient.getOption("agentId"), agentClient.getOption("agentDn"), event.startTime, customerNumber, event.oriDnis, "1", "-1");
            //agentLog.sendMessage(agentLog.RINGING, customerNumber, "-1", "1");
        //}
    };

    //话路接通
    agentClient.onConnected = function(event) {
        if(event.lastStatus == 31){ //咨询通话接通后取消
            if(isTransfer) {
                transferCancelSuccess(); //转接咨询取消
            }
            if(isConference) {
                conferenceCancelSuccess(); //会议咨询取消
            }
        } else if (event.lastStatus == 29) { //保持取回后导致的话路接通，直接忽略掉

        } else if(event.lastStatus == 33) { //会议目标方退出
            var otherParty = event.otherParty;
            if (otherParty.indexOf("9") == 0 && otherParty.length > 7) { //去掉外呼前缀为9的
                otherParty = otherParty.substr(1);
            }
            if (otherParty.indexOf("01") == 0 && otherParty.length == 12) { //以外地手机开头
                otherParty = otherParty.substr(1);
            }
            conferenceQuitSuccess(otherParty);
        } else {
            if(isAdviceCancel) { //如果是取消咨询导致再次调用了此方法，则直接忽略掉
                isAdviceCancel = false;
            } else {
                $("#acceptPhoneBtn").hide();
                $("#releasePhoneBtn").show();
                $("#signOutBtn, #readyBtn, #statusMenuBtn, #dailPhoneBtn").attr("disabled", true);
                $("#holdPhoneBtn, #transferPhoneBtn, #skillGroupBtn, #conferencePhoneBtn").attr("disabled", false);
                $("#statusInfo").html("通话");
                //停掉当前的计时器
                window.clearInterval(timer);
                $("#callTime").html("00:00");
                timerDirection = "up";
                seconds = 1;
                timer = setInterval("startTimer()", 1000);
               /* var customerNumber = event.oriAni; // 原始主叫号码
                if (customerNumber.indexOf("01") == 0 && customerNumber.length == 12) { //去掉外外地手机前面加拨的0
                    customerNumber = customerNumber.substr(1);
                }*/

                // agentLog.sendMessage(agentLog.ESTABLISHED);
            }
        }
    };

    //话路释放
    agentClient.onReleased = function(event) {
        if(event.lastStatus == 31) { //咨询通话结束
            if(isTransfer) {
                transferSuccess(); //转接成功
            }
            if(isConference) { //会议成功
                conferenceSuccess();
            }
        }

        $("#signInBtn").hide();
        $("#signOutBtn").show().attr("disabled", false);
        $("#readyBtn, #dailPhoneBtn").attr("disabled", false);
        $("#statusMenuBtn").attr("disabled", false).children("span").text("话后处理");

        $("#acceptPhoneBtn").show().attr("disabled", true);
        $("#releasePhoneBtn").hide();
        $("#holdPhoneBtn").show().attr("disabled", true);
        $("#recoveryPhoneBtn").hide();
        $("#transferPhoneBtn, #skillGroupBtn, #conferencePhoneBtn").attr("disabled", true);

        $("#statusInfo").html("话后处理");
        $("#outPhoneNumber").html("无");
        $("#inPhoneNumber").html("无");
        released(lastRecordId, event.contactId, event.startTime,$("#callTime").html());
        //停掉当前的计时器
        window.clearInterval(timer);
        $("#callTime").html("00:00");
        timerDirection = "down";
        seconds = 120;
        timer = setInterval("startTimer()", 1000);


        // agentLog.sendMessage(agentLog.RELEASED);

        $("#callingStatus").empty();
    };

    //录音结束
    agentClient.onRecordEnd = function(event) {

    };

    //出错
    agentClient.onError = function(event) {
        console.log("err:");
        console.log(event);
        if(event.errorCode === 1343225881){
            layer.msg("目标正忙，请稍后再拨！");
        }
    };

    //启动客户端
    agentClient.start();

});

/**
 * 坐席签入
 * @param params
 * @returns {boolean}
 */
function signIn(params) {
    if(!params.agentDn || !params.agentId){
        layer.msg("工号或者分机号为空！");
        return false;
    }

    agentClient.setOption({
        agentId: params.agentId,
        agentDn: params.agentDn,
        agentName: params.agentName
    });

    return agentClient.login();
}

/**
 * 坐席签出
 * @returns {*}
 */
function signOut(){
    return agentClient.logout();
}

/**
 * 坐席就绪
 * @returns {*}
 */
function ready() {
    return agentClient.ready();
}

/**
 * 坐席置忙
 * @returns {*}
 */
function notReady() {
    return agentClient.notReady()
}

/**
 * 坐席小休
 * @returns {*}
 */
function rest() {
    return agentClient.otherWork(70);
}

/**
 * 坐席培训
 * @returns {*}
 */
function train() {
    return agentClient.otherWork(71);
}

/**
 * 坐席就餐
 * @returns {*}
 */
function meal() {
    return agentClient.otherWork(72);
}

/**
 * 接听
 * @returns {*}
 */
function answer() {
    return agentClient.answerCall();
}

/**
 * 挂断
 * @returns {*}
 */
function release(){
    return agentClient.hangupCall();
}

/**
 * 保持
 * @returns {*}
 */
function hold(){
    return agentClient.holdCall();
}

/**
 * 恢复
 * @returns {*}
 */
function held(){
    return agentClient.retrieveCall();
}

/**
 * 转接咨询
 * @param number 咨询号码
 * @param callType 呼叫类型
 * @returns {boolean}
 */
function advice(number, callType){
    if(!agentClient.getOption("isLogin")){
        layer.msg("当前未签入，无法拨打电话！");
        return false;
    }

    if(agentClient.getOption("status") != 28){
        layer.msg("当前状态无法进行咨询操作！");
        return false;
    }

    if(number.length == 0) {
        layer.msg("请输入所要咨询的号码！");
        return false;
    }

    if(callType == 3){ //外线
        var localAreaCode = $("#localAreaCode").val();//当地区号
        if(number.indexOf(localAreaCode) == 0){ //以区号开头，则去掉区号
            number = number.substr(localAreaCode.length);
        }
    }

    if(agentClient.consultCall(parseInt(callType), number)){
        isTransfer = true;
    } else {
        layer.msg("转接咨询失败！");
    }
}

/**
 * 会议咨询
 * @param number 咨询号码
 * @param callType 呼叫类型
 * @returns {boolean}
 */
function meetingAdvice(number, callType){
    if(!agentClient.getOption("isLogin")){
        layer.msg("当前未签入，无法拨打电话！");
        return false;
    }

    if(agentClient.getOption("status") != 28){
        layer.msg("当前状态无法进行咨询操作！");
        return false;
    }

    if(number.length == 0) {
        layer.msg("请输入所要咨询的号码！");
        return false;
    }

    if(callType == 3){ //外线
        var localAreaCode = $("#localAreaCode").val();//当地区号
        if(number.indexOf(localAreaCode) == 0){ //以区号开头，则去掉区号
            number = number.substr(localAreaCode.length);
        }
    }

    if(agentClient.consultCall(parseInt(callType), number)){
        isConference = true;
    } else {
        layer.msg("转接咨询失败！");
    }
}

/**
 * 取消转接
 * @returns {*}
 */
function transferCancel() {
    return agentClient.reconnectCall();
}

/**
 * 确认转接
 * @returns {*}
 */
function transferOk(){
    return agentClient.forwardCall();
}

/**
 * 会议取消
 * @returns {*}
 */
function meetingCancel() {
    return agentClient.reconnectCall();
}

/**
 * 会议确认
 * @returns {*}
 */
function meetingOk() {
    return agentClient.joinCall();
}

/**
 * 转接技能组
 * @param skillGroupKey
 * @returns {*}
 */
function transferSkillGroup(skillGroupKey){
    if(skillGroupKey) {
        return agentClient.singleStepTransfer('4', skillGroupKey);
    }

    return false;
}

/**
 * 签入成功
 */
function loginSuccess(){
    $("#signInBtn").hide();
    $("#signOutBtn").show();
    $("#readyBtn, #dailPhoneBtn").attr("disabled", false);
    $("#statusMenuBtn").attr("disabled", false).children("span").text("置忙");
    $("#currentStatus").text("已签入");
    $("#loginRunNumber").text(agentClient.getOption("agentDn"));
    $("#loginUserNumber").text(agentClient.getOption("agentId"));
    $("#statusInfo").text("置忙");

    if(!agentLog){
        agentLog = new AgentLog({
            userId: $("#userId").val() || "0",
            userName: $("#userName").val() || "0",
            agentId: agentClient.getOption("agentId"),
            tenantsId: $("#loginTenantsId").val() || "0",
            agentDn: agentClient.getOption("agentDn"),
            proxyUrl: "agentLog/insert"
        });
    }

    agentLog.sendMessage(agentLog.LOGIN);
    layer.msg("签入成功！");
}

/**
 * 签出成功
 */
function logoutSuccess(){
    $("#signInBtn").show();
    $("#signOutBtn").hide();
    $("#readyBtn, #acceptPhoneBtn, #dailPhoneBtn," +
        " #holdPhoneBtn, #transferPhoneBtn, #skillGroupBtn, #conferencePhoneBtn").attr("disabled", true);

    $("#statusMenuBtn").attr("disabled", true).children("span").text("状态");
    $("#currentStatus").text("未签入");
    $("#loginRunNumber").text("未知");
    $("#loginUserNumber").text("未知");
    $("#statusInfo").text("未知");

    agentLog.sendMessage(agentLog.LOGOUT);
    layer.msg("签出成功！");
}

/**
 * 置忙成功
 */
function notReadySuccess(){
    $("#readyBtn, #dailPhoneBtn").attr("disabled", false);
    $("#statusMenuBtn").attr("disabled", false).children("span").text("置忙");
    $("#acceptPhoneBtn,#holdPhoneBtn, #transferPhoneBtn, #skillGroupBtn, #conferencePhoneBtn").attr("disabled", true);

    agentLog.sendMessage(agentLog.BUSY);
    $("#statusInfo").text("置忙");
    //停止倒计时
    stopTimer();
}

/**
 * 就绪成功
 */
function readySuccess(){
    $("#readyBtn").attr("disabled", true);
    $("#statusMenuBtn").attr("disabled", false).children("span").text("就绪");
    $("#acceptPhoneBtn, #dailPhoneBtn," +
        " #holdPhoneBtn, #transferPhoneBtn, #skillGroupBtn, #conferencePhoneBtn").attr("disabled", true);

    agentLog.sendMessage(agentLog.READY);
    $("#statusInfo").text("就绪");
    //停止倒计时
    stopTimer();
}

/**
 * 小休成功
 */
function restSuccess(){
    $("#readyBtn, #dailPhoneBtn").attr("disabled", false);
    $("#statusMenuBtn").attr("disabled", false).children("span").text("小休");
    $("#acceptPhoneBtn, #dailPhoneBtn," +
        " #holdPhoneBtn, #transferPhoneBtn, #skillGroupBtn, #conferencePhoneBtn").attr("disabled", true);

    agentLog.sendMessage(agentLog.REST);
    $("#statusInfo").text("小休");
    //停止倒计时
    stopTimer();
}


/**
 * 话后处理成功
 */
function afterCallWorkSuccess(){
    $("#signOutBtn, #readyBtn, #dailPhoneBtn").attr("disabled", false);
    $("#statusMenuBtn").attr("disabled", false).children("span").text("话后处理");

    agentLog.sendMessage(agentLog.AFTER_CALL_WORK);
    $("#statusInfo").text("话后处理");
}

/**
 * 培训成功
 */
function trainSuccess(){
    $("#readyBtn, #dailPhoneBtn").attr("disabled", false);
    $("#statusMenuBtn").attr("disabled", false).children("span").text("培训");
    $("#acceptPhoneBtn, #dailPhoneBtn," +
        " #holdPhoneBtn, #transferPhoneBtn, #skillGroupBtn, #conferencePhoneBtn").attr("disabled", true);

    agentLog.sendMessage(agentLog.TRAIN);
    $("#statusInfo").text("培训");
    //停止倒计时
    stopTimer();
}

/**
 * 就餐成功
 */
function mealSuccess(){
    $("#readyBtn, #dailPhoneBtn").attr("disabled", false);
    $("#statusMenuBtn").attr("disabled", false).children("span").text("就餐");
    $("#acceptPhoneBtn, #dailPhoneBtn," +
        " #holdPhoneBtn, #transferPhoneBtn, #skillGroupBtn, #conferencePhoneBtn").attr("disabled", true);

    agentLog.sendMessage(agentLog.MEAL);
    $("#statusInfo").text("就餐");
    //停止倒计时
    stopTimer();
}

/**
 * 保持成功
 */
function holdSuccess(){
    $("#holdPhoneBtn").hide();
    $("#recoveryPhoneBtn").show();

    agentLog.sendMessage(agentLog.HOLD);
}

/**
 * 保持恢复成功
 */
function retrievedSuccess(){
    $("#holdPhoneBtn").show();
    $("#recoveryPhoneBtn").hide();

    agentLog.sendMessage(agentLog.RECOVERY);
}

/**
 * 转接咨询成功
 */
function adviceSuccess(){
    agentLog.sendMessage(agentLog.TRANSFER);
    $("#skillGroupBtn, #conferencePhoneBtn").attr("disabled", true);
    $("#statusInfo").html("转接咨询");
    //13127908653
    layer.msg("正在进行转接咨询！");
}

/**
 * 会议咨询成功
 */
function conferenceAdviceSuccess(){
    agentLog.sendMessage(agentLog.CONFERENCE);
    $("#transferPhoneBtn, #skillGroupBtn").attr("disabled", true);
    $("#statusInfo").html("会议咨询");
    layer.msg("正在进行会议咨询！");
}

/**
 * 转接咨询通话成功
 */
function adviceConnectedSuccess(){
    agentLog.sendMessage(agentLog.ESTABLISHED);
    $("#statusInfo").html("转接咨询通话");
    layer.msg("转接咨询已接通！");
}

/**
 * 会议咨询通话成功
 */
function conferenceConnectedSuccess(){
    agentLog.sendMessage(agentLog.ESTABLISHED);
    $("#statusInfo").html("会议咨询通话");
    layer.msg("会议咨询已接通！");
}


/**
 * 转接成功
 */
function transferSuccess(){
    agentLog.sendMessage(agentLog.TRANSFER_SUCCESS);
    layer.msg("已成功转接给的对方！");
    isTransfer = false;
}

/**
 * 会议成功
 */
function conferenceSuccess(){
    $("#acceptPhoneBtn").hide();
    $("#releasePhoneBtn").show().attr("disabled", false);
    $("#holdPhoneBtn").show().attr("disabled", true);
    $("#recoveryPhoneBtn").hide();
    layer.msg("目标方已成功加入会议！");
    isConference = false;
}

/**
 * 转接取消成功
 */
function transferCancelSuccess(){
    agentLog.sendMessage(agentLog.TRANSFER_CANCEL);
    $("#holdPhoneBtn").show();
    $("#recoveryPhoneBtn").hide();
    $("#skillGroupBtn, #conferencePhoneBtn").attr("disabled", false);
    $("#statusInfo").html("通话");
    layer.msg("转接已取消！");
    isTransfer = false;
}

/**
 * 会议取消成功
 */
function conferenceCancelSuccess(){
    agentLog.sendMessage(agentLog.CONFERENCE_CANCEL);
    $("#holdPhoneBtn").show();
    $("#recoveryPhoneBtn").hide();
    $("#transferPhoneBtn, #skillGroupBtn").attr("disabled", false);
    $("#statusInfo").html("通话");
    layer.msg("会议已取消！");
    isConference = false;
}

/**
 * 目标方退出会议成功
 * @param quit
 */
function conferenceQuitSuccess(quit){
    $("#acceptPhoneBtn").hide();
    $("#releasePhoneBtn").show();
    $("#signOutBtn, #readyBtn, #statusMenuBtn, #dailPhoneBtn").attr("disabled", true);
    $("#holdPhoneBtn, #transferPhoneBtn, #skillGroupBtn, #conferencePhoneBtn").attr("disabled", false);
    $("#statusInfo").html("通话");
    layer.msg("目标【" + quit + "】已退出会议！");
}

/**
 * 启动计时器
 */
function startTimer() {
    if(seconds <= 0 && timerDirection == "down"){
        //自动置忙软电话
        notReady();
    }
    var minuteStr, secondStr;

    var minute = parseInt(seconds / 60);
    var remainder = seconds % 60;
    if(minute < 10){
        minuteStr = "0" + minute;
    } else {
        minuteStr = minute;
    }

    if(remainder < 10){
        secondStr = "0" + remainder;
    } else {
        secondStr = remainder;
    }

    $("#callTime").html(minuteStr + ":" + secondStr);

    if(timerDirection == "up"){
        seconds++;
    }
    if(timerDirection == "down"){
        seconds--;
    }
}

/**
 * 停止计时器
 */
function stopTimer() {
    if(timer){
        window.clearInterval(timer);
        $("#callTime").html("00:00");
    }
}

/**
 * 软电话拨号事件
 * @param calledNumber 呼叫号码
 * @param callType 呼叫类型
 * @param callPrefix 外呼前缀
 */
function dailModalCall(calledNumber, callType, callPrefix){
    if(!agentClient.getOption("isLogin")){
        layer.msg("当前未签入，无法拨打电话！");
        return false;
    } else if (agentClient.getOption("status") == 3 //在就绪、小休、培训、就餐及话后处理时，需要先置忙再外呼
        || agentClient.getOption("status") == 7
        || agentClient.getOption("status") == 6) {

        notReady(); //先置忙
        if (calledNumber.length > 0) {
            if (callType == 3) { //外线拨号
                //以本地区号开头，则去掉区号
                var localAreaCode = $("#localAreaCode").val();//当地区号
                if(calledNumber.indexOf(localAreaCode) == 0){
                    calledNumber = calledNumber.substr(localAreaCode.length);
                }
            }
            if (agentClient.makeCall(parseInt(callType), calledNumber, callPrefix)) {

            } else {
                layer.msg("呼叫失败，请联系管理员！");
            }
        } else {
            layer.msg("请输入呼叫号码！");
        }

        return false;
    } else if (agentClient.getOption("status") == 2){ //置忙时直接外呼
        if (calledNumber.length > 0) {
            if (callType == 3) { //外线拨号
                //以本地区号开头，则去掉区号
                var localAreaCode = $("#localAreaCode").val();//当地区号
                if(calledNumber.indexOf(localAreaCode) == 0){
                    calledNumber = calledNumber.substr(localAreaCode.length);
                }
            }
            if (agentClient.makeCall(parseInt(callType), calledNumber)) {

            } else {
                layer.msg("呼叫失败，请联系管理员！");
            }
        } else {
            layer.msg("请输入呼叫号码！");
        }

        return false;
    } else {
        layer.msg("当前状态不允许拨打电话！");

        return false;
    }
}

/**
 * 用户信息面板直接拨号事件
 * @param calledNumber 外呼号码
 * @param type 类型
 * @param customerId 客户id
 * @param callPrefix 外呼前缀
 * @returns {boolean}
 */
function outDailCall(calledNumber, type, customerId, callPrefix){
    if(!agentClient.getOption("isLogin")){
        layer.msg("当前未签入，无法拨打电话！");
        return false;
    }
    if (agentClient.getOption("status") == 3 //在就绪、小休、培训、就餐及话后处理时，需要先置忙再外呼
        || agentClient.getOption("status") == 7
        || agentClient.getOption("status") == 6) {

        notReady(); //先置忙
        if(calledNumber.length > 0){
            if(type == "2"){
                //以1开头且长度为11则为手机号码
                if(calledNumber.indexOf("1") == 0 && calledNumber.length == 11){
                    calledNumber = "0" + calledNumber;
                }
            } else if(type == "1") {
                //以本地区号开头，则去掉区号
                var localAreaCode = $("#localAreaCode").val();//当地区号
                if(calledNumber.indexOf(localAreaCode) == 0){
                    calledNumber = calledNumber.substr(localAreaCode.length);
                }
            }
            if(agentClient.makeCall(3, calledNumber, callPrefix)) {

            } else {
                layer.msg("呼叫失败！");
            }
        } else {
            layer.msg("呼叫号码无效！");
        }
    } else if (agentClient.getOption("status") == 2){
        if(calledNumber.length > 0){
            if(type == "2"){
                //以1开头且长度为11则为手机号码
                if(calledNumber.indexOf("1") == 0 && calledNumber.length == 11){
                    calledNumber = "0" + calledNumber;
                }
            } else if(type == "1") {
                //以本地区号开头，则去掉区号
                var localAreaCode = $("#localAreaCode").val();//当地区号
                if(calledNumber.indexOf(localAreaCode) == 0){
                    calledNumber = calledNumber.substr(localAreaCode.length);
                }
            }
            if(agentClient.makeCall(3, calledNumber, callPrefix)) {

            } else {
                layer.msg("呼叫失败！");
            }
        } else {
            layer.msg("呼叫号码无效！");
        }
    } else {
        layer.msg("当前状态不允许拨打电话！");
    }

    if(layer.index){
        layer.close(layer.index);
    }

    return false;
}

/**
 * 快速拨号
 * @param callNumber 号码
 * @param callPrefix 拨号前缀
 * @param customerId 用户id
 */
function speedDial(callNumber, callPrefix, customerId){
    if(!agentClient.getOption("isLogin")){
        layer.msg("当前未签入，无法拨打电话！",{
            zIndex: Math.MAX_VALUE
        });
        return false;
    }
    if (agentClient.getOption("status") == 3 //在就绪、小休、培训、就餐及话后处理时，需要先置忙再外呼
        || agentClient.getOption("status") == 7
        || agentClient.getOption("status") == 6) {

        notReady(); //先置忙
        layer.open({
            title: "快速拨号",
            type: 1,
            shadeClose: true,
            shade: 0.8,
            area: ["300px", "380px"],
            anim: 3,
            content:
            "<div style='padding:10px;text-align:center;'>" +
            "   <button onclick=\"outDailCall('" + callNumber + "','1','" + customerId + "','" + callPrefix + "');\" class='layui-btn layui-btn-big' title='市内拨号' style='width:80px;height:80px;padding:0px;padding-top:8px;'><i class='layui-icon' style='font-size:40px;'>&#xe650;</i><div style='margin-top:-10px;'>市内拨号</div></button> " +
            "   <button onclick=\"outDailCall('" + callNumber + "','2','" + customerId + "','" + callPrefix + "');\" class='layui-btn layui-btn-big' title='长途拨号' style='width:80px;height:80px;padding:0px;padding-top:8px;'><i class='layui-icon' style='font-size:40px;'>&#xe650;</i><div style='margin-top:-10px;'>长途拨号</div></button>" +
            "</div>" +
            "<div style='padding:10px;text-align:center;'>" +
            "   <button onclick=\"outDailCall('" + callNumber + "','1','" + customerId + "','06');\" class='layui-btn layui-btn-normal layui-btn-big' title='中梦市内' style='width:80px;height:80px;padding:0px;padding-top:8px;'><i class='layui-icon' style='font-size:40px;'>&#xe650;</i><div style='margin-top:-10px;'>中梦市内</div></button> " +
            "   <button onclick=\"outDailCall('" + callNumber + "','2','" + customerId + "','06');\" class='layui-btn layui-btn-normal layui-btn-big' title='中梦长途' style='width:80px;height:80px;padding:0px;padding-top:8px;'><i class='layui-icon' style='font-size:40px;'>&#xe650;</i><div style='margin-top:-10px;'>中梦长途</div></button>" +
            "</div>" +
            "<div style='padding:10px;text-align:center;'>" +
            "   <button onclick=\"outDailCall('" + callNumber + "','1','" + customerId + "','04');\" class='layui-btn layui-btn-warm layui-btn-big' title='大都会市内' style='width:80px;height:80px;padding:0px;padding-top:8px;'><i class='layui-icon' style='font-size:40px;'>&#xe650;</i><div style='margin-top:-10px;'>大都会市内</div></button> " +
            "   <button onclick=\"outDailCall('" + callNumber + "','2','" + customerId + "','04');\" class='layui-btn layui-btn-warm layui-btn-big' title='大都会长途' style='width:80px;height:80px;padding:0px;padding-top:8px;'><i class='layui-icon' style='font-size:40px;'>&#xe650;</i><div style='margin-top:-10px;'>大都会长途</div></button>" +
            "</div>"
        });
    } else if (agentClient.getOption("status") == 2){
        layer.open({
            title: "快速拨号",
            type: 1,
            shadeClose: true,
            shade: 0.8,
            area: ["300px", "380px"],
            anim: 3,
            content:
            "<div style='padding:10px;text-align:center;'>" +
            "   <button onclick=\"outDailCall('" + callNumber + "','1','" + customerId + "','" + callPrefix + "');\" class='layui-btn layui-btn-big' title='市内拨号' style='width:80px;height:80px;padding:0px;padding-top:8px;'><i class='layui-icon' style='font-size:40px;'>&#xe650;</i><div style='margin-top:-10px;'>市内拨号</div></button> " +
            "   <button onclick=\"outDailCall('" + callNumber + "','2','" + customerId + "','" + callPrefix + "');\" class='layui-btn layui-btn-big' title='长途拨号' style='width:80px;height:80px;padding:0px;padding-top:8px;'><i class='layui-icon' style='font-size:40px;'>&#xe650;</i><div style='margin-top:-10px;'>长途拨号</div></button>" +
            "</div>" +
            "<div style='padding:10px;text-align:center;'>" +
            "   <button onclick=\"outDailCall('" + callNumber + "','1','" + customerId + "','06');\" class='layui-btn layui-btn-normal layui-btn-big' title='中梦市内' style='width:80px;height:80px;padding:0px;padding-top:8px;'><i class='layui-icon' style='font-size:40px;'>&#xe650;</i><div style='margin-top:-10px;'>中梦市内</div></button> " +
            "   <button onclick=\"outDailCall('" + callNumber + "','2','" + customerId + "','06');\" class='layui-btn layui-btn-normal layui-btn-big' title='中梦长途' style='width:80px;height:80px;padding:0px;padding-top:8px;'><i class='layui-icon' style='font-size:40px;'>&#xe650;</i><div style='margin-top:-10px;'>中梦长途</div></button>" +
            "</div>" +
            "<div style='padding:10px;text-align:center;'>" +
            "   <button onclick=\"outDailCall('" + callNumber + "','1','" + customerId + "','04');\" class='layui-btn layui-btn-warm layui-btn-big' title='大都会市内' style='width:80px;height:80px;padding:0px;padding-top:8px;'><i class='layui-icon' style='font-size:40px;'>&#xe650;</i><div style='margin-top:-10px;'>大都会市内</div></button> " +
            "   <button onclick=\"outDailCall('" + callNumber + "','2','" + customerId + "','04');\" class='layui-btn layui-btn-warm layui-btn-big' title='大都会长途' style='width:80px;height:80px;padding:0px;padding-top:8px;'><i class='layui-icon' style='font-size:40px;'>&#xe650;</i><div style='margin-top:-10px;'>大都会长途</div></button>" +
            "</div>"
        });
    } else {
        layer.msg("当前状态不允许拨打电话！",{
            zIndex: Math.MAX_VALUE
        });
    }

    return false;
}